package com.ptbuy.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.telephony.TelephonyManager;


import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.ptbuy.base.BaseApplication;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {


    /**
     * 返回带有逗号的数字
     *
     * @param str
     * @return
     */
    public static String getNumlStr(String str) {
        String replace = str.replace(",", "");
        BigDecimal result = new BigDecimal(replace);
        String format = String.valueOf(result);
        long l = Long.parseLong(format);
        DecimalFormat df1 = new DecimalFormat("#,###");
        String format1 = df1.format(l);
        return format1;
    }

    /**
     * 处理价格问题
     *
     * @param price
     * @return
     */
    private static String dealPrice(Double price) {
        String format2 = String.format("%.3f", price);
        String numlPrice;
        if (format2.contains(".")) {
            String[] split = format2.split("\\.");
            String numlLongsplit = getNumlStr(split[0]);
            numlPrice = numlLongsplit + "." + split[1];
        } else {
            numlPrice = getNumlStr(String.valueOf(price));
        }
        return numlPrice;
    }

    /**
     * 获取时间戳
     *
     * @return
     */
    public static String getCurrentTime(Date date) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
//        Log.d(TAG, df.format(date));
            return df.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    public static String stringToTime(String stringTime) {
        String time = "";
        if (stringTime != null) {
            try {
                time = stringTime.substring(0, 13);
                Date date = new SimpleDateFormat("yyyyMMddHHmm").parse(time);
                time = new SimpleDateFormat("yyyy年MM月dd日HH点mm分").format(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return time;
    }


    public static String getVersion() {

        try {
            PackageManager manager = BaseApplication.getContext().getPackageManager();
            PackageInfo info = manager.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getFileMD5(InputStream in) throws NoSuchAlgorithmException, IOException {

        MessageDigest digest;
        byte buffer[] = new byte[1024];
        int len;
        digest = MessageDigest.getInstance("MD5");
        while ((len = in.read(buffer, 0, 1024)) != -1) {
            digest.update(buffer, 0, len);
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        in.close();
        return bigInt.toString(16);
    }


    public static String getDeviceId() {
        @SuppressLint("HardwareIds") String string = Settings.Secure.getString(BaseApplication.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        return string;
    }


    // 检查是否有所有文件访问权限
    public static boolean hasAllFilesAccess() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            return Environment.isExternalStorageManager();
        }
        return true; // 在 Android 10 及更早版本中不需要特别检查
    }
}
